package com.able.concurrency.t1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abel.he
 * @data 2018/8/15 20:37
 * @describe
 */
public class Test16_2 {

    public static void main(String[] args) {
        final Contina1 contina = new Contina1();

        new Thread(new Runnable() {
            public void run() {
                synchronized (contina) {
                    if (contina.size() != 5) {
                        try {
                            System.out.println("进入等待线程状态");
                            contina.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("第一个线程数量已经确定为5了 可以结束线程了");
                    contina.notifyAll();
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                synchronized (contina) {
                    for (int i = 0; i < 10; i++) {
                        System.out.println("第一个线程容器数量为：" + contina.size());
                        if (contina.size() == 5) {
                            try {
                                contina.notifyAll();
                                contina.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        contina.add(new Object());
                    }
                }
            }
        }).start();

    }

}

class Contina{
    List<Object> list = new ArrayList<Object>();

    void add(Object o) {
        list.add(o);
    }
    int size() {
        return list.size();
    }
}
